package cn.tedu.baidashi.controller.back;

import cn.tedu.baidashi.common.ResponseCode;
import cn.tedu.baidashi.model.Category;
import cn.tedu.baidashi.service.ICategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/categories")
public class BackCategoryController {

    @Resource
    private ICategoryService categoryService;

    /**
     * 获得分类集合
     * @return
     */
    @GetMapping
    public List<Category> getCategories(){
        List<Category> categories=categoryService.findCategories();
        return categories;
    }

    /**
     * 根据分类id修改分类名称
     * @param catId
     * @param catName
     * @return
     */
    @GetMapping("/update/{catId}/{catName}")
    public Category updateCategory(@PathVariable Integer catId,@PathVariable String catName){
        Category category=categoryService.updateCategoryById(catId, catName, LocalDateTime.now());
        return category;
    }

    /**
     * 根据分类id删除类别
     * @param catId
     * @return
     */
    @GetMapping("/delete/{catId}")
    public String deleteCategory(@PathVariable Integer catId){
        categoryService.deleteCategoryById(catId);
        return ResponseCode.RESPONSE_CODE_SUCCESS.getMessage();
    }

    /**
     * 新增分类
     * @param catName
     * @return
     */
    @PostMapping("/add")
    public Category addCategory(String catName){
        LocalDateTime time=LocalDateTime.now();
        Category category=new Category();
        category.setCatName(catName)
                .setCreatedAt(time)
                .setUpdatedAt(time);
        return categoryService.addCategory(category);
    }

}
